package com.fjh.springboot_01.mapper;

import com.fjh.springboot_01.pojo.homework.Homework;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface HomeworkMapper {
    @Insert("INSERT INTO homework (tutoring_booking_id, title, description, homework_type, " +
            "status, category_id, create_time, update_time) " +
            "VALUES (#{tutoringBookingId}, #{title}, #{description}, #{homeworkType}, " +
            "#{status}, #{categoryId}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Homework homework);

    @Select("SELECT h.*, u1.username as teacher_name, u2.username as student_name " +
            "FROM homework h " +
            "LEFT JOIN tutoring_booking tb ON h.tutoring_booking_id = tb.id " +
            "LEFT JOIN user u1 ON tb.teacher_id = u1.id " +
            "LEFT JOIN user u2 ON tb.student_id = u2.id " +
            "WHERE h.id = #{id}")
    @Results({
        @Result(column = "teacher_name", property = "teacherName"),
        @Result(column = "student_name", property = "studentName")
    })
    Homework findById(Integer id);

    @Select("SELECT h.*, u1.username as teacher_name, u2.username as student_name " +
            "FROM homework h " +
            "LEFT JOIN tutoring_booking tb ON h.tutoring_booking_id = tb.id " +
            "LEFT JOIN user u1 ON tb.teacher_id = u1.id " +
            "LEFT JOIN user u2 ON tb.student_id = u2.id " +
            "WHERE tb.teacher_id = #{teacherId} " +
            "ORDER BY h.create_time DESC")
    @Results({
        @Result(column = "teacher_name", property = "teacherName"),
        @Result(column = "student_name", property = "studentName")
    })
    List<Homework> findByTeacherId(Integer teacherId);

    @Select("SELECT h.*, u1.username as teacher_name, u2.username as student_name " +
            "FROM homework h " +
            "LEFT JOIN tutoring_booking tb ON h.tutoring_booking_id = tb.id " +
            "LEFT JOIN user u1 ON tb.teacher_id = u1.id " +
            "LEFT JOIN user u2 ON tb.student_id = u2.id " +
            "WHERE tb.student_id = #{studentId} " +
            "ORDER BY h.create_time DESC")
    @Results({
        @Result(column = "teacher_name", property = "teacherName"),
        @Result(column = "student_name", property = "studentName")
    })
    List<Homework> findByStudentId(Integer studentId);

    @Update("UPDATE homework SET status = #{status}, update_time = NOW() " +
            "WHERE id = #{id}")
    void updateStatus(@Param("id") Integer id, @Param("status") Integer status);
} 